Fix: Support nested struct field filtering with PyArrow (#953)#2628
Merged
Fokko merged 5 commits intoapache:mainfrom Nov 11, 2025
Merged
Fix: Support nested struct field filtering with PyArrow (#953)#2628Fokko merged 5 commits intoapache:mainfrom
Fokko merged 5 commits intoapache:mainfrom
Conversation
Fixes filtering on nested struct fields when using PyArrow for scan operations.
## Problem
When filtering on nested struct fields (e.g., `mazeMetadata.run_id == 'value'`),
PyArrow would fail with:
```
ArrowInvalid: No match for FieldRef.Name(run_id) in ...
```
The issue occurred because PyArrow requires nested field references as tuples
(e.g., `("parent", "child")`) rather than dotted strings (e.g., `"parent.child"`).
## Solution
1. Modified `_ConvertToArrowExpression` to accept an optional `Schema` parameter
2. Added `_get_field_name()` method that converts dotted field paths to tuples
for nested struct fields
3. Updated `expression_to_pyarrow()` to accept and pass the schema parameter
4. Updated all call sites to pass the schema when available
## Changes
- `pyiceberg/io/pyarrow.py`:
- Modified `_ConvertToArrowExpression` class to handle nested field paths
- Updated `expression_to_pyarrow()` signature to accept schema
- Updated `_expression_to_complementary_pyarrow()` signature
- `pyiceberg/table/__init__.py`:
- Updated call to `_expression_to_complementary_pyarrow()` to pass schema
- Tests:
- Added `test_ref_binding_nested_struct_field()` for comprehensive nested field testing
- Enhanced `test_nested_fields()` with issue apache#953 scenarios
## Example
```python
# Now works correctly:
table.scan(row_filter="mazeMetadata.run_id == 'abc123'").to_polars()
```
The fix converts the field reference from:
- ❌ `FieldRef.Name(run_id)` (fails - field not found)
- ✅ `FieldRef.Nested(FieldRef.Name(mazeMetadata) FieldRef.Name(run_id))` (works!)
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
Contributor
Author
|
@Fokko what do you think of this change? |
|
Just chiming in to say I've encountered this same bug and it's blocking my ability to filter tables with struct columns. Appreciate your fix 🙏 I hope this can be merged soon. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #953
Rationale for this change
Fixes filtering on nested struct fields when using PyArrow for scan operations.
Are these changes tested?
Yes, the full test suite + new tests
Are there any user-facing changes?
Now, filtering a scan using a nested field will work
Problem
When filtering on nested struct fields (e.g.,
parentField.childField == 'value'), PyArrow would fail with:The issue occurred because PyArrow requires nested field references as tuples (e.g.,
("parent", "child")) rather than dotted strings (e.g.,"parent.child").Solution
_ConvertToArrowExpressionto accept an optionalSchemaparameter_get_field_name()method that converts dotted field paths to tuples for nested struct fieldsexpression_to_pyarrow()to accept and pass the schema parameterChanges
pyiceberg/io/pyarrow.py:_ConvertToArrowExpressionclass to handle nested field pathsexpression_to_pyarrow()signature to accept schema_expression_to_complementary_pyarrow()signaturepyiceberg/table/__init__.py:_expression_to_complementary_pyarrow()to pass schematest_ref_binding_nested_struct_field()for comprehensive nested field testingtest_nested_fields()with issue Query on nested struct field with PyIceberg? #953 scenariosExample
The fix converts the field reference from:
FieldRef.Name(run_id)(fails - field not found)FieldRef.Nested(FieldRef.Name(mazeMetadata) FieldRef.Name(run_id))(works!)